package com.mayikt.base;

import com.mayikt.constants.Constants;
import lombok.Data;
import org.springframework.stereotype.Component;

/**
 * @Auther: CHEN JIN LONG
 * @Date: 2019/1/18 21:39
 * @Description:
 */

@Data
@Component
public class BaseApiService<T> {

    /**
     * 返回错误
     *
     * @return
     */
    public BaseResponse<T> setResultError() {
        return setResult(Constants.HTTP_RES_CODE_500, Constants.HTTP_RES_CODE_500_VALUE, null);
    }

    /**
     * 返回错误
     *
     * @param code
     * @return
     */
    public BaseResponse<T> setResultError(Integer code) {
        return setResult(code, Constants.HTTP_RES_CODE_500_VALUE, null);
    }

    /**
     * 返回错误
     *
     * @param msg
     * @return
     */
    public BaseResponse<T> setResultError(String msg) {
        return setResult(Constants.HTTP_RES_CODE_500, msg, null);
    }

    /**
     * 返回错误
     *
     * @param code
     * @param msg
     * @return
     */
    public BaseResponse<T> setResultError(Integer code, String msg) {
        return setResult(code, msg, null);
    }

    /**
     * 返回成功
     *
     * @return
     */
    public BaseResponse<T> setResultSuccess() {
        return setResult(Constants.HTTP_RES_CODE_200, Constants.HTTP_RES_CODE_200_VALUE, null);
    }

    /**
     * 返回成功
     *
     * @param data
     * @return
     */
    public BaseResponse<T> setResultSuccess(T data) {
        return setResult(Constants.HTTP_RES_CODE_200, Constants.HTTP_RES_CODE_200_VALUE, data);
    }

    /**
     * 返回成功
     *
     * @param msg
     * @return
     */
    public BaseResponse<T> setResultSuccess(String msg) {
        return setResult(Constants.HTTP_RES_CODE_200, msg, null);
    }


    /**
     * 通用封装类
     *
     * @param code
     * @param msg
     * @param data
     * @return
     */
    public BaseResponse<T> setResult(Integer code, String msg, T data) {
        return new BaseResponse(code, msg, data);
    }
}
